<?php namespace web\home\controller;
/** .-------------------------------------------------------------------
 * |  Software: [HDPHP framework]
 * |      Site: www.hdphp.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

class Show extends Conf_common {
	//首页
	public function index() {
//		p($_SESSION);
//		获取gid
		$gid=Q('get.gid',0,'intval');		
//		属性数据
		$oldData=Db::table('goods_attr')->join('type_attr','goods_attr.taid','=','type_attr.taid')
		->where('type_attr.class',0)->where('gid',$gid)->get();
		$num=count($oldData);
		$num=$num/2;
		$arr1=array_slice($oldData,0,$num);
		$arr2=array_slice($oldData,$num);
//		$oldData=array_merge($arr1,$arr2);
		$arrData['arr1']=$arr1;
		$arrData['arr2']=$arr2;
//		p($arrData);die;
		View::with('arrData',$arrData);
		
//		商品数据
		$data=Db::table('goods')->join('detail','goods.gid','=','detail.gid')->where('goods.gid',$gid)->first();
		$data['small']=explode('|',$data['small']);
		$data['medium']=explode('|',$data['medium']);
		$data['big']=explode('|',$data['big']);
		$data['kucun']=array_sum(Db::table('goods_list')->where('gid',$gid)->lists('inventory'));
		View::with('data',$data);
//		货品规格
		$attrData=Db::table('goods_attr')->join('type_attr','goods_attr.taid','=','type_attr.taid')
		->where('gid',$gid)->where('type_attr.class',1)->groupBy('goods_attr.taid')->get();
		
		foreach($attrData as $k=>$v){
			$attrData[$k]['Son']=Db::table('goods_attr')->where('gid',$gid)->where('taid',$v['taid'])->orderBy('gtid','ASC')->get();
		}
		View::with('attrData',$attrData);

		View::make();
	}
//		查询库存
	public function inven(){
		$gid=Q('post.gid',0,'intval');
		$com=rtrim(Q('post.combine',''),'|');
		$specData=Db::table('goods_list')->where('gid',$gid)->where('combine',$com)->pluck('inventory');
		message($specData,0,'success');
	}
	
//	加入购物车
	public function shoplist(){
		$gid=Q('post.gid',0,'intval');
		$num=Q('post.num',0,'intval');
//		获取尺寸颜色
		$com=rtrim(Q('post.combine',''),'|');
		$com=explode('|',$com);
		$newData=array();
		foreach($com as $k=>$v){
			$data=Db::table('goods_attr')
			->join('type_attr','goods_attr.taid','=','type_attr.taid')
			->where('goods_attr.gtid',$v)
			->lists('taname,gtvalue');
			$newData=array_merge($newData,$data);
		}

//		获取商品数据
		$goodsData=Db::table('goods')->where('gid',$gid)->first();
		$shoplist=array(
			'gid'=>$goodsData['gid'],
			'gname'=>$goodsData['gname'],
			'shopprice'=>$goodsData['shopprice'],
			'pic'=>$goodsData['pic'],
			'ongg'=>$comData,
			'num'=>$num,
		);
		
//		写入session
		
		$data = [ 
		    'id'=> $goodsData['gid'], // 商品 ID 
		    'name'=>$goodsData['gname'],// 商品名称 
		    'num'=> $num, // 商品数量 
		    'price'=> $goodsData['shopprice'], // 商品价格 
		    'pic'=>$goodsData['pic'],//商品图片
		    'options'=> array(
		    	'combine'=>$com,
		    	'com'=>$newData,
		    ),
		]; 
		\Cart::add($data); // 添加到购物车 
//		统计购物车数量
		$shopnum=\Cart::getTotalNums();
		message($shopnum,0,'success');
	}
//	查询购物车数据
	public function shopeach(){
		$oldshop=\Cart::getGoods();
		message($oldshop,'','success');
	}
	
//	编辑购物车
	public function edit(){
//		p($_POST);
		$sid=Q('post.sid','');
		$num=Q('post.num',0,'intval');
//		p($num);die;
		$data=array( 
    		'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
    		'num'=>$num 
		); 
		\Cart::update($data);
//		当前数据获取数据
		$oldshop=\Cart::getGoods();
		foreach($oldshop as $k=>$v){
			if($k==$sid){
				$shop=$v;
			}
		}
//		p($shop);die;
		$shopnum=\Cart::getTotalNums();
		$shop['shopnum']=$shopnum;
//		p($shopnum);die;
		message($shop,'','success');
	}
//	删除购物车商品
	public function del(){
//		p($_POST);
		$sid=Q('post.sid','');
//		p($num);die;
		$data=array( 
    		'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
    		'num'=>0 
		); 
		\Cart::update($data);
		$shopnum=\Cart::getTotalNums();
		message($shopnum,'','success');
	}
	
//	购物车列表页
	public function jiesuan(){
//		分配购物车数据
		$oldshop=\Cart::getGoods();
		View::with('oldshop',$oldshop);
//		p($oldshop);
		
//		p($_SESSION);die;
		View::make();
	}
	
//	统计已选择商品总数量和总价格
	public function tongji(){
//		p($_POST);die;
		$data=explode(',',$_POST['sid']);
		$oldshop=\Cart::getGoods();
//		p($data);die;
//		echo 2;die;
		if(current($data)){
			foreach ($data as $key => $value) {
				foreach ($oldshop as $k => $v) {
					if($value==$k){
						$zj[]=$v['total'];
						$zs[]=$v['num'];
					}
				}
			}	
			$oldData['zj']=array_sum($zj);
			$oldData['zs']=array_sum($zs);
			message($oldData,'','success');
			
		}
		message('0','','success');
		
	}
	
	public function zhifu(){
//		接收数据
		if(IS_POST){
			$data=explode(',',$_POST['sid']);
		$oldshop=\Cart::getGoods();
		if(current($data)){
			foreach ($data as $key => $value) {
				foreach ($oldshop as $k => $v) {
					if($value==$k){
						$_SESSION['shop']['goods'][]=$v;
						$zj[]=$v['total'];
						$zs[]=$v['num'];
					}
				}
			}	
			$_SESSION['shop']['zj']=array_sum($zj);
			$_SESSION['shop']['zs']=array_sum($zs);
			message($oldData,'','success');
			
			return;
		}
		message('0','','error');
		}
//		p($_SESSION['shop']);
		if(!$_SESSION['id']){
			header("Location:U('home/user/login')"); 
		}
		
		$userData=Db::table('user')->where('uid',$_SESSION['id'])->first();
		$userData['address2']=explode(',',$userData['address']);
		View::with('userData',$userData);
		
//		p($userData);		
//		View::with('session',$_SESSION);
		View::with('shopData',$_SESSION['shop']);
		View::make();
	}
	
	
	
	
	
}































